package com.taijihuabao.modules.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.DeptEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.mapper.DeptMapper;
import com.taijihuabao.modules.mapper.DeptRoleMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import com.taijihuabao.modules.model.dept.dept.DeptDTO;
import com.taijihuabao.modules.model.dept.dept.DeptPageDTO;
import com.taijihuabao.modules.model.dept.role.DeptRoleDTO;
import com.taijihuabao.modules.model.dept.user.DeptUserDTO;
import com.taijihuabao.modules.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 部门管理
 * @author Xiao
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper deptMapper;

    @Resource
    private DeptRoleMapper deptRoleMapper;

    @Override
    public PageInfo selectAllDept(DeptPageDTO deptDTO) throws ServiceException{
        List<DeptEntity> list = deptMapper.selectAllDept(deptDTO);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_FOUND);
        }
        //获取子、父部门的名称
        for(DeptEntity deptEntity : list){
            String parentDept = deptEntity.getParentDept();
            if(!StringUtils.isEmpty(parentDept)){
                DeptEntity deptEntity1 = deptMapper.selectDeptByCode(parentDept);
                if(!Objects.isNull(deptEntity1)){
                    deptEntity.setParentName(deptEntity1.getDeptName());
                }
            }
            String subDept = deptEntity.getSubDept();
            if(!StringUtils.isEmpty(subDept)){
                DeptEntity deptEntity2 = deptMapper.selectDeptByCode(subDept);
                if(!Objects.isNull(deptEntity2)) {
                    deptEntity.setSubName(deptEntity2.getDeptName());
                }
            }
        }
        PageHelper.startPage(deptDTO.getPage(), deptDTO.getSize());
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<DeptEntity> selectDepts() throws ServiceException {
        List<DeptEntity> list = deptMapper.selectDepts();
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_FOUND);
        }
        for(DeptEntity deptEntity : list) {
            //同级子部门
            List<DeptEntity> subs = deptMapper.selectDeptByParentCode(deptEntity.getDeptCode());
            if (!CollectionUtils.isEmpty(subs)) {
                deptEntity.setChildren(subs);
                for (DeptEntity sub : subs) {
                    if(!StringUtils.isEmpty(sub.getParentDept())){
                        sub.setParentName(deptEntity.getDeptName());
                    }
                    if (!StringUtils.isEmpty(sub.getDeptCode())) {
                        findSubDept(subs);
                    }
                }
            }
        }
        //去重
        for (int i = 0; i < list.size(); i++) {
            String parentDept = list.get(i).getParentDept();
            if (!StringUtils.isEmpty(parentDept)) {
                list.remove(list.get(i));
                i--;
            }
        }
        return list;
    }

    /**
     * 递归循环寻找部门子级
     * @param subs
     * @return
     */
    public void findSubDept(List<DeptEntity> subs){
        for(DeptEntity sub : subs){
            List<DeptEntity> subDepts = deptMapper.selectDeptByParentCode(sub.getDeptCode());
            if(!CollectionUtils.isEmpty(subDepts)){
                sub.setChildren(subDepts);
                for(DeptEntity subDept : subDepts){
                    if(!StringUtils.isEmpty(subDept.getParentDept())) {
                        subDept.setParentName(sub.getDeptName());
                    }
                    if(!StringUtils.isEmpty(subDept.getDeptCode())){
                        findSubDept(subDepts);
                    }
                }
            }
        }
    }

    @Override
    public DeptEntity selectDeptByCode(String deptCode) throws ServiceException{
        DeptEntity deptEntity = deptMapper.selectDeptByCode(deptCode);
        if(Objects.isNull(deptEntity)){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        //获取子、父部门的名称
        if(!StringUtils.isEmpty(deptEntity.getParentDept())){
            DeptEntity parent = deptMapper.selectDeptByCode(deptEntity.getParentDept());
            if(!Objects.isNull(parent)){
                deptEntity.setParentName(parent.getDeptName());
            }
        }
        if(!StringUtils.isEmpty(deptEntity.getSubDept())){
            DeptEntity sub = deptMapper.selectDeptByCode(deptEntity.getSubDept());
            if(!Objects.isNull(sub)){
                deptEntity.setSubName(sub.getDeptName());
            }
        }
        return deptEntity;
    }

    @Override
    public DeptEntity selectDeptById(Long id) throws ServiceException{
        DeptEntity deptEntity = deptMapper.selectDeptById(id);
        if(Objects.isNull(deptEntity)){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        //获取子、父部门的名称
        if(!StringUtils.isEmpty(deptEntity.getParentDept())){
            DeptEntity parent = deptMapper.selectDeptByCode(deptEntity.getParentDept());
            if(!Objects.isNull(parent)){
                deptEntity.setParentName(parent.getDeptName());
            }
        }
        if(!StringUtils.isEmpty(deptEntity.getSubDept())){
            DeptEntity sub = deptMapper.selectDeptByCode(deptEntity.getSubDept());
            if(!Objects.isNull(sub)){
                deptEntity.setSubName(sub.getDeptName());
            }
        }
        return deptEntity;
    }

    @Override
    public  List<DeptEntity> selectDeptByName(String deptName) throws ServiceException{
        List<DeptEntity> list = deptMapper.selectDeptByName(deptName);
        if (CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_FOUND);
        }
        //获取子、父部门的名称
        for(DeptEntity deptEntity : list){
            if(!StringUtils.isEmpty(deptEntity.getParentDept())){
                DeptEntity parent = deptMapper.selectDeptByCode(deptEntity.getParentDept());
                if(!Objects.isNull(parent)){
                    deptEntity.setParentName(parent.getDeptName());
                }
            }
            if(!StringUtils.isEmpty(deptEntity.getSubDept())){
                DeptEntity sub = deptMapper.selectDeptByCode(deptEntity.getSubDept());
                if(!Objects.isNull(sub)){
                    deptEntity.setSubName(sub.getDeptName());
                }
            }
        }
        return list;
    }

    @Override
    public int insertDept(DeptDTO deptEntity) throws ServiceException{
        //判断部门id是否重复
        if(!Objects.isNull(deptMapper.selectDeptByCode(deptEntity.getDeptCode()))){
            throw new ServiceException(ApplicationResponseCode.DEPT_HAS_EXIST);
        }
        //新增部门的同时可以赋部门角色
        int result = 0;
        if(!CollectionUtils.isEmpty(deptEntity.getRoleList())){
            //新增部门
            result = deptMapper.insertDept(deptEntity);
            for(int i=0;i<deptEntity.getRoleList().size();i++){
                //赋部门角色给部门
                DeptRoleDTO deptRoleDTO = new DeptRoleDTO();
                deptRoleDTO.setDeptCode(deptEntity.getDeptCode());
                deptRoleDTO.setDeptRole(deptEntity.getRoleList().get(i));
                if(1>deptRoleMapper.insertDept(deptRoleDTO)){
                    throw new ServiceException(ApplicationResponseCode.DEPT_INSERT_FAILED);
                }
            }
        }else{
            //新增部门
            result = deptMapper.insertDept(deptEntity);
        }
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_INSERT_FAILED);
        }
        return result;
    }

    @Override
    public int updateDept(DeptDTO deptEntity) throws ServiceException{
        //判断部门id是否存在
        if(Objects.isNull(deptMapper.selectDeptByCode(deptEntity.getDeptCode()))){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        int result = deptMapper.updateDept(deptEntity);
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_UPDATE_FAILED);
        }
        return result;
    }

    @Override
    public Integer deleteDept(DeptDTO deptEntity) throws ServiceException{
        //判断部门id是否存在
        if(Objects.isNull(deptMapper.selectDeptById(deptEntity.getId()))){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        Long mark = 1L;
        if(!mark.equals(deptMapper.selectDeptById(deptEntity.getId()).getDeleteMark())){
            throw new ServiceException(ApplicationResponseCode.DEPT_DELETE_EXISTED);
        }
        int result = deptMapper.deleteDept(deptEntity);
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_DELETE_FAILED);
        }
        return result;
    }

}
